<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function wait(n) {
            return new Promise((res, rej) => {
                setTimeout(() => {
                    res('等待了' + n + '毫秒')
                }, n)
            })
        }
        // console.log('start')
        // function waitFunc() {
        //     return wait(1000)
        //         .then(res => {
        //             console.log('-', res)
        //             return wait(2000)
        //         })
        //         .then(res => {
        //             console.log('-', res)
        //             return wait(3000)
        //         })
        //         .then(res => {
        //             console.log('-', res)
        //         })
        // }
        // console.log('end')
        // waitFunc()

        // async function waitfuncasnyc() {
        //     const res1 = await wait(1000)
        //     console.log('=', res1)
        //     const res2 = await wait(2000)
        //     console.log('=', res2)
        //     const res3 = await wait(3000)
        //     console.log('=', res3)
        // }
        waitfuncasnyc()
        function waitfuncasnyc() {
            return wait(1000)
                .then(res => {
                    console.log(res)
                    return wait(2000)
                })
                .then(res => {
                    console.log(res)
                    return wait(3000)
                })
                .then(res => {
                    console.log(res)
                })
        }

        // Promise.all([wait(1000), wait(2000), wait(3000)]).then(console.log)
        // Promise.race([wait(1000), wait(2000), wait(3000)]).then(console.log)


        const promise = new Promise((resolve, reject) => {
            console.log(1); // 1
            resolve(5); // 4
            console.log(2); // 2
        }).then(val => {
            console.log(val);
        });

        promise.then(() => {
            console.log(3); // 5
        });

        console.log(4); // 3

        setTimeout(function () {
            console.log(6); // 6
        });
    </script>
</body>

</html>